package com.j3l11234.familyvote.core;



import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import javax.management.timer.Timer;

import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;




public class FamilityVote {
  private long totalVote;
  
  private HttpGet cookieIndex;
  private HttpGet vote;
  private int proxyListIndex;

  public FamilityVote() {
    
    cookieIndex = new HttpGet("http://menhu.huilan.com/vsite/ui/smallApp/vote/cookieIndex.htm?activityId=c275e1dcf44b4dacbf6f2518024ef480");
    FamilityVote.setHeader(cookieIndex);
    
    vote = new HttpGet("http://menhu.huilan.com/vsite/ui/smallApp/vote/doVote.htm?itemId=289eb510b5ae4164abaa36150d968c03");
    FamilityVote.setHeader(vote);
    vote.addHeader("Referer", "http://menhu.huilan.com/vsite/ui/smallApp/vote/cookieIndex.htm?activityId=c275e1dcf44b4dacbf6f2518024ef480&snsId=&userType=");
  }
  
  public void _start() {
    for (long j = 0; j < 10; j++) {
      new Thread(voteRun).start();
    }
    
    new java.util.Timer().scheduleAtFixedRate(new TimerTask() {
      private long last;
      public void run() {
        long totalVote2 = totalVote;
        System.out.println(totalVote2+","+(totalVote2-last)/10);
        last = totalVote2;
      }
    },0, 10000);
  }

  Runnable voteRun = new Runnable() {
    public void run() {
      while (true) {
          try { 
            CloseableHttpClient httpclient = HttpClients.createDefault();
            CloseableHttpResponse cookieIndexResponse = httpclient.execute(cookieIndex);
            cookieIndexResponse.close();  
            for (int i = 0; i < 250; i++) {
              for (int j = 0; j < 20; j++) {
                CloseableHttpResponse voteResponse = httpclient.execute(vote);
                String retult = EntityUtils.toString(voteResponse.getEntity());
                if(!retult.equals("0")){
                  i = 999;
                  j = 000;
                  break;
                }
                voteResponse.close();  
              }
              synchronized (this) {
                totalVote += 20;
              }
            }
            
          }catch(Exception e){
            System.err.println(e.getClass().getName()+","+e.getMessage());
          }
        
      }
    }
  };
  
  public static void setHeader(HttpGet httpGet){
    httpGet.setHeader("Host", "menhu.huilan.com");
    httpGet.setHeader("User-Agent", "Mozilla/5.0 MicroMessenger/6.1.0.66_r1062275.542");  
  }
  
//  public static void main(String[] args) {
//    FamilityVote  fv = new FamilityVote();
//    fv.start();
//  }
}